package com.hyts.shell.ftp;


import java.io.File;
import java.io.FileOutputStream;
import java.util.*;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

import static java.lang.Integer.parseInt;

/**
 * FTP下载工具
 * @author zhangweichao
 * @date 2020/5/7
 */
public class FTPUtil {

    /**
     * FTP地址
     **/
    private String address;
    /**
     * FTP端口
     **/
    private Integer port;
    /**
     * FTP用户名
     **/
    private String username;
    /**
     * FTP密码
     **/
    private String password;

    private String filePath;
    private String fileName;

    private String savePath;

    public FTPUtil() {
    }

    public String getFilePath() {
        return filePath;
    }

    public String getFileName() {
        return fileName;
    }

    public String getSavePath() {
        return savePath;
    }

    private ChannelSftp sftp= new ChannelSftp();

    private JSch jsch = new JSch();
    /**
     * 连接sftp服务器
     * host 主机
     * @ port 端口
     * @ username 用户名
     * @ password 密码
     * @return
     */
    public void connectFtp() throws Exception{


        if (jsch ==null ){
            jsch = new JSch();
        }
        Session sshSession = jsch.getSession(username, address, port);
        sshSession.setPassword(password);
        Properties sshConfig = new Properties();
        sshConfig.put("StrictHostKeyChecking", "no");
        sshSession.setConfig(sshConfig);
        sshSession.connect();
        Channel channel = sshSession.openChannel("sftp");
        channel.connect();
        if (sftp ==null ){
            sftp = (ChannelSftp) channel;
        }
    }


    public static void main(String[] args){
        try {
            FTPUtil ftpUtil = new FTPUtil();
            ftpUtil.username = "root";
            ftpUtil.port = 22;
            ftpUtil.password = "HAfb7jio@CMzh";
            ftpUtil.address = "192.168.2.205";
            ftpUtil.connectFtp();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void download() throws Exception{
        sftp.cd(filePath);
        File file = new File(savePath+ fileName);
        sftp.get(fileName, new FileOutputStream(file));
    }

    /**
     *
     *  parse ftp addresss
     * @param addr ftp://root:jt123@@172.26.3.112:22/home/test/movie.xml
     */
    private int parseFtpAddr(String addr) throws Exception{
        if (addr.isEmpty()){
            return -1;
        }
        String cutftp = addr.replaceFirst("ftp://","");
        String res[] = cutftp.split(":", 2);
        username=res[0];
        int len = res[1].lastIndexOf("@");
        password = res[1].substring(0,len);
        String re[] = res[1].split("/", 2);
        int index =  re[0].indexOf(":");
        address = re[0].substring(password.length()+1,index);
        port= parseInt (re[0].substring(index+1,(re[0]).length()));
        int lenpath = re[1].lastIndexOf("/");
        filePath= "/"+  re[1].substring(0,lenpath);
        fileName= re[1].substring(lenpath+1,(re[1].length()));
        return 1;
    }

    /**
     *
     * @param addr
     * @return  xml filepath + filename
     */
    public void downloadByFtpAddr(String addr) throws Exception{
        int i = parseFtpAddr(addr);
        if (i > 0){
            connectFtp();
            download();
        }
    }
}